﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using InfoTech.VinhSon.Bussiness;
using InfoTech.VinhSon.Models;
using Telerik.Web.Mvc;

#endregion

namespace VinhSon.Areas.Administrator.Controllers
{
    public class UserController : BaseAdministratorController
    {
        public ActionResult Index()
        {
            return View();
        }

        [GridAction]
        public ActionResult GetList()
        {
            return View(new GridModel(UnitOfWork.UserRepository.Select(c => c.Inactive == false || c.Inactive == null)));
        }

        public ActionResult Add()
        {
            return View(new UserModel());
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Add(UserModel model)
        {
            if (ModelState.IsValid)
            {
                User webUser = model;
                if (UnitOfWork.UserRepository.CheckInsertUser(webUser))
                {
                    UnitOfWork.UserRepository.Insert(webUser);
                    UnitOfWork.Save();
                    return RedirectToAction("Index");
                }
                ModelState.AddModelError("error",
                                            "Username or Email has exists. Please try again");
            }
            return View(model);
        }

        public ActionResult Edit(string id)
        {
            int guid;
            if (int.TryParse(id,
                            out guid))
            {
                User webUser = UnitOfWork.UserRepository.SelectbyId(guid);
                if (webUser != null)
                {
                    UserEditModel webUserModel = webUser;
                    return View(webUserModel);
                }
            }
            return RedirectToAction("Index");
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(UserEditModel model)
        {
            if (ModelState.IsValid)
            {
                User webUser = UnitOfWork.UserRepository.SelectbyId(model.Id);
                model.Update(webUser);
                if (UnitOfWork.UserRepository.CheckUpdateUser(webUser))
                {
                    UnitOfWork.UserRepository.Update(webUser);
                    UnitOfWork.Save();
                    return RedirectToAction("Index");
                }
                ModelState.AddModelError("error",
                                            "Username or Email has exists. Please try again");
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult Delete(string id)
        {
            if (id != null)
            {
                string[] s = id.Split(',');
                int guid;
                List<int> list = (from s1 in s
                                  where int.TryParse(s1,
                                                      out guid)
                                  select int.Parse(s1)).ToList();

                if (UnitOfWork.UserRepository.CheckDeleteUser(list))
                {
                    UnitOfWork.UserRepository.Delete(list);
                    UnitOfWork.Save();
                }
            }
            return Json(new
                            {
                                status = 1,
                                msg = "Update Successfully"
                            },
                        JsonRequestBehavior.DenyGet);
        }

        public ActionResult Logout()
        {
            Session.Clear();
            FormsAuthentication.SignOut();
            HttpCookie httpCookie = Response.Cookies[FormsAuthentication.FormsCookieName];
            if (httpCookie != null)
                httpCookie.Expires = DateTime.Now.AddYears(-1);
            return Redirect("/");
        }
    }
}
